package co.msk8.server.rpc;

import java.util.HashMap;
import java.util.Map;

import co.msk8.client.command.Action;
import co.msk8.client.command.Result;
import co.msk8.client.rpc.CalculatorService;
import co.msk8.server.action.ActionHandler;
import co.msk8.server.action.commands.SumCommand;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class CalculatorServiceImpl extends RemoteServiceServlet implements CalculatorService {

	/** serial. */
	private static final long serialVersionUID = -7147520317575013980L;
	
	private Map<Class<? extends Action<? extends Result>>, ActionHandler> commands;
	
	
	public CalculatorServiceImpl() {
		
		//Comandos injectados:
		SumCommand sumCmd = new SumCommand();
		
		commands = new HashMap<Class<? extends Action<? extends Result>>, ActionHandler>();
		commands.put(sumCmd.getActionType(), sumCmd);
	}

	public <T extends Result> T execute(Action<T> action) throws Exception {
		return (T) commands.get(action.getClass()).execute(action);
	}

}
